package cn.xyy.system.service;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.xyy.system.dao.PermissionDao;
import cn.xyy.system.dao.RoleDao;
import cn.xyy.web.AuthUserHolder;
import cn.xyy.common.domain.system.auth.Permission;
import cn.xyy.common.domain.system.auth.PermissionVO;
import cn.xyy.common.domain.system.auth.RolePermission;
import cn.xyy.common.utils.BeanUtils;
import cn.xyy.common.utils.DateUtils;
import cn.xyy.common.utils.StringUtils;
import cn.xyy.common.exception.SystemException;

/**
 * 权限业务层
 * 
 * @author xyy
 *
 */
@Service
public class PermissionService {

	@Autowired
	private PermissionDao permissionDao;
	@Autowired
	private RoleDao roleDao;

	public List<Permission> getPermissionList() {
		return permissionDao.selectAll();
	}

	public List<Permission> getEnablePermissionList() {
		return getPermissionList().stream().filter(e -> 1 == e.getStatus()).collect(Collectors.toList());
	}

	public List<Permission> getPermissionListByRole(String[] roleIds) {
		if (roleIds == null) {
			throw new SystemException("角色roleIds为空");
		}
		List<Permission> permissionList = permissionDao.selectByRole(roleIds);
		if (permissionList == null || permissionList.isEmpty()) {
			return null;
		}
		return permissionList.stream().distinct().collect(Collectors.toList());
	}

	public void add(PermissionVO vo) {
		if (vo.getTypes() == null) {
			throw new SystemException("权限类型不能为空");
		}
		if (StringUtils.isBlank(vo.getTitle())) {
			throw new SystemException("菜单名称/按钮名称不能为空");
		}
		if (vo.getTypes() != 2) {
			if (StringUtils.isBlank(vo.getPath())) {
				throw new SystemException("路由地址不能为空");
			}
		}
		if (vo.getTypes() == 1 && vo.getIframe() == 0) {
			if (StringUtils.isBlank(vo.getName())) {
				throw new SystemException("组件名不能为空");
			}
			if (StringUtils.isBlank(vo.getComponent())) {
				throw new SystemException("组件路径不能为空");
			}
		}

		if (vo.getParentId() == null) {
			vo.setParentId("0");
		}
		if (vo.getTypes() == null) {
			vo.setTypes(2);
		}
		if (vo.getHide() == null) {
			vo.setParentId("2");
		}
		if (vo.getHideChildren() == null) {
			vo.setHideChildren(2);
		}
		if (vo.getIframe() == null) {
			vo.setIframe(2);
		}
		if (vo.getStatus() == null) {
			vo.setStatus(1);
		}
		if (vo.getCache() == null) {
			vo.setCache(2);
		}

		Permission permission = new Permission();
		BeanUtils.copyProperties(vo, permission);
		permission.setCreator(AuthUserHolder.getUserInfo().getUsername());
		permission.setCreateTime(DateUtils.getDateTime());

		int result = permissionDao.insert(permission);
		if (result < 0) {
			throw new SystemException("操作失败");
		}
	}

	public void update(PermissionVO vo) {
		if (vo.getTypes() == null) {
			throw new SystemException("权限类型不能为空");
		}
		if (StringUtils.isBlank(vo.getTitle())) {
			throw new SystemException("菜单标题/按钮名称不能为空");
		}
		if (vo.getTypes() != 2) {
			if (StringUtils.isBlank(vo.getPath())) {
				throw new SystemException("路由地址不能为空");
			}
		}
		if (vo.getTypes() == 1 && vo.getIframe() == 0) {
			if (StringUtils.isBlank(vo.getName())) {
				throw new SystemException("组件名不能为空");
			}
			if (StringUtils.isBlank(vo.getComponent())) {
				throw new SystemException("组件路径不能为空");
			}
		}
		if (vo.getParentId() == null) {
			vo.setParentId("0");
		}
		Permission permission = permissionDao.selectById(vo.getId(), Permission.class);
		if (permission == null) {
			throw new SystemException("未查询到对应的权限");
		}
		BeanUtils.copyProperties(vo, permission);
		permission.setUpdater(AuthUserHolder.getUserInfo().getUsername());
		permission.setUpdateTime(DateUtils.getDateTime());

		int result = permissionDao.update(permission);
		if (result < 0) {
			throw new SystemException("执行失败");
		}
	}

	@Transactional(value = "baseTransactionManager")
	public void delete(String id) {
		if (id == null) {
			throw new SystemException("删除条件不能为空");
		}
		// 删除角色权限
		roleDao.delete(new RolePermission().setPermId(id));

		// 删除权限本身
		permissionDao.delete(new Permission().setId(id));

	}

}
